﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Mall.Infrastructure.Model;

namespace Mall.Infrastructure.EnityFramework
{
    /// <summary>
    /// 分页扩展
    /// </summary>
    public static class PagingExtensions
    {
        public static IEnumerable<TSource> Paging<TSource>(this IOrderedQueryable<TSource> source, Page page)
        {
            //获取总页数
            page.Count= source.Count();

            //页面大小(最小为1)
            page.PageSize = page.PageSize < 1 ? 1 : page.PageSize;
            //页面总数
            if (page.Count % page.PageSize == 0)
            {
                page.PageCount = page.Count / page.PageSize;
            }
            else
            {
                page.PageCount = page.Count / page.PageSize+1;
            }
            //当前页面(最大)
            page.CurrentPage = page.CurrentPage > page.Count ? page.Count : page.CurrentPage;
            //当前页面(最小)
            page.CurrentPage=page.CurrentPage<1?1:page.CurrentPage;
            var result = source.Skip((page.CurrentPage-1) * page.PageSize).Take(page.PageSize);
            return result;
        }
    }
}
